﻿using ProductSystem.BLL.Common;
using ProductSystem.Model;
using Fundway.Common.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace ProductSystem.BLL
{
    public partial class System_User_InfoBll
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="curpage">当前页</param>
        /// <param name="pagesize">每页记录数</param>
        /// <param name="recordCount">总数</param>
        /// <returns></returns>
        public static List<System_User_Info> GetList(string cond, int curpage, int pagesize, ref int recordCount)
        {
            List<System_User_Info> list = new List<System_User_Info>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                if (string.IsNullOrEmpty(cond))
                {
                    list = dbContext.System_User_Infos.Where(m => m.Level != 0 && m.Status == 1).OrderByDescending(m => m.ID).Select(t => t).Skip((curpage - 1) * pagesize).Take(pagesize).ToList();
                    recordCount = dbContext.System_User_Infos.Where(m => m.Status == 1).Count();
                }
                else
                {
                    list = dbContext.System_User_Infos.Where(m => m.Level != 0 && m.Status == 1 && (m.Account.Contains(cond) || m.User_Name.Contains(cond))).OrderByDescending(m => m.ID).Select(t => t).Skip((curpage - 1) * pagesize).Take(pagesize).ToList();
                    recordCount = dbContext.System_User_Infos.Where(m => m.Status == 1 && (m.Account.Contains(cond) || m.User_Name.Contains(cond))).Count();
                }
            }
            return list;
        }

        public static List<System_User_Info> GetList(string cond, ref PageInfo pageInfo)
        {
            List<System_User_Info> list = new List<System_User_Info>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                if (string.IsNullOrEmpty(cond))
                {
                    list = dbContext.System_User_Infos.Where(m => m.Level != 0 && m.Status == 1).OrderByDescending(m => m.ID).ToList();

                }
                else
                {
                    list = dbContext.System_User_Infos.Where(m => m.Level != 0 && m.Status == 1 && (m.Account.Contains(cond) || m.User_Name.Contains(cond))).OrderByDescending(m => m.ID).ToList();
                }
                pageInfo.TotalCount = list.Count;
                list = list.Skip((pageInfo.CurPage - 1) * pageInfo.PageSize).Take(pageInfo.PageSize).ToList();
            }
            return list;
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="status">状态</param>
        /// <returns>List</returns>
        public static List<System_User_Info> GetList(string User)
        {
            List<System_User_Info> list = new List<System_User_Info>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                if (string.IsNullOrEmpty(User))
                    list = dbContext.System_User_Infos.Where(m => m.Level != 0 && m.Status == 1).ToList();
                else
                    list = dbContext.System_User_Infos.Where(m => m.Level != 0 && m.Status == 1 && (m.Account.Contains(User) || m.User_Name.Contains(User))).ToList();
            }
            return list;
        }

        /// <summary>
        /// 是否存用户账号
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="id">用户ID</param>
        /// <returns>true/false</returns>
        public static bool ExitAccount(string account, int id)
        {
            bool reStr = false;
            int total = 0;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                if (id == 0)
                {
                    total = dbContext.System_User_Infos.Where(m => m.Account == account && m.Status != 0 && m.ID != id).Count();
                }
                else
                {
                    total = dbContext.System_User_Infos.Where(m => m.Account == account && m.Status != 0 && m.ID != id).Count();
                }
                reStr = total == 0 ? false : true;
            }
            return reStr;
        }

        public static bool GetUserFuntion(int userId)
        {
            bool reStatus = false;
            List<System_Menu> listTemp = new List<System_Menu>();
            string cacheKeyCode = "UserFunction-" + userId;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                System_User_Info user = dbContext.System_User_Infos.FirstOrDefault(m => m.ID == userId);
                if (user != null && user.Level == 0)
                {
                    listTemp = System_MenuBll.GetList(1);
                }
                else
                {
                    listTemp = dbContext.ExecuteQuery<System_Menu>("Exec Sp_User_Function " + userId).ToList();
                }
            }
            CacheRepository.AddOrReplace(cacheKeyCode, listTemp, CacheExpirationTypes.RelativelyStable);
            return reStatus;
        }

        /// <summary>
        /// 根据用户账号获取用户真实姓名
        /// </summary>
        /// <param name="account">用户账号</param>
        /// <returns>用户真实姓名</returns>
        public static string GetUserNameBy(string account)
        {
            string userName = "未知";
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                var useInfo = dbContext.System_User_Infos.FirstOrDefault(o => o.Account == account);
                if (useInfo != null)
                {
                    userName = useInfo.User_Name;
                }
                return userName;
            }
        }

        /// <summary>
        /// 获取用户信息的下拉框数据源
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <returns></returns>
        public static SelectList GetSelectList(string UserName)
        {
            List<System_User_Info> list;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                list = dbContext.System_User_Infos.Where(o => o.Status == 1).ToList();
            }
            return new SelectList(list, "User_Name", "User_Name", UserName);
        }
    }
}
